package com.dsunsoft.rtu.service.impl;

import com.dsunsoft.rtu.common.util.CommonUtils;
import com.dsunsoft.rtu.dao.RtuLogMapper;
import com.dsunsoft.rtu.entity.RtuLog;
import com.dsunsoft.rtu.service.RtuLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Slf4j
@Service
public class RtuLogServiceImpl implements RtuLogService {

    @Autowired
    private RtuLogMapper mapper;

    @Transactional
    @Override
    public void save(String content, String errMsg) {
        Integer deviceId = Integer.parseInt(content.substring(4, 8), 16);
        RtuLog rtuLog = new RtuLog();
        rtuLog.setId(CommonUtils.getUuid());
        rtuLog.setDeviceId(String.valueOf(deviceId));
        rtuLog.setContent(content);
        rtuLog.setErrorMessage(errMsg);
        rtuLog.setCreatedDate(new Date());
        mapper.insert(rtuLog);
    }
}
